/*******************************************************************/
/* @ All Rights Reserved Copyright (C) 2014 broada,Co.,Ltd.        */
/*                                                                 */
/*  NAME      = t_thrift_tbinary_detect.c                          */
/*  NOTE      =                                                    */
/*  DATE      = 2017/02/17 by jikw                                 */
/*******************************************************************/

#include <CUnit/Basic.h>
#include <stdlib.h>

#include "frcomm.h"
#include "frmem.h"
#include "conntrack.h"
#include "frct_comm.h"
#include "../../../../src/module/thrift/config.h"

extern int thrift_tbinary_detect(Conntrack *p_st_conn, Apm_packet *p_st_packet);

static int __suite_init(void)
{

	return 0;
}

static int __suite_cleanup(void)
{

	return 0;
}

//NULL
static void case1(void)
{
    Conntrack *p_st_conn = NULL;
    Apm_packet *p_st_packet = NULL;

    int i_ret = 0;

    i_ret = thrift_tbinary_detect(p_st_conn, p_st_packet);

    CU_ASSERT_EQUAL(i_ret, APM_L7_IS_NO);

    return;
}

//request
static void case2(void)
{
    Conntrack *p_st_conn = NULL;
    Apm_packet *p_st_packet = NULL;
    int i_ret = 0;

    p_st_conn = (Conntrack *)frmalloc(sizeof(Conntrack));
    if(p_st_conn == NULL)
        goto ERROR;
    memset(p_st_conn, 0x00, sizeof(Conntrack));

    p_st_packet = (Apm_packet *)frmalloc(sizeof(Apm_packet));
    if(p_st_packet == NULL)
        goto ERROR;
    memset(p_st_packet, 0x00, sizeof(Apm_packet));
    p_st_packet->p_payload = (char *)"\x80\x01\x00\x01\x00\x00\x00\x12\x74\x72\x61\x6e\x73\x56\x61\x6c\x69\x64\x61\x74\x65\x3a\x69\x73\x4f\x6b\x00\x00\x00\x01\x00";
    p_st_packet->ui_payload_len = 31;

    i_ret = thrift_tbinary_detect(p_st_conn, p_st_packet);

    CU_ASSERT_EQUAL(i_ret, APM_L7_IS_YES);

    frfree(p_st_conn);
    frfree(p_st_packet);

    return;

ERROR:
    if(p_st_conn)
        frfree(p_st_conn);
    if(p_st_packet)
        frfree(p_st_packet);

    return;
}

//response
static void case3(void)
{
    Conntrack *p_st_conn = NULL;
    Apm_packet *p_st_packet = NULL;
    int i_ret = 0;

    p_st_conn = (Conntrack *)frmalloc(sizeof(Conntrack));
    if(p_st_conn == NULL)
        goto ERROR;
    memset(p_st_conn, 0x00, sizeof(Conntrack));

    p_st_packet = (Apm_packet *)frmalloc(sizeof(Apm_packet));
    if(p_st_packet == NULL)
        goto ERROR;
    memset(p_st_packet, 0x00, sizeof(Apm_packet));
    p_st_packet->p_payload = (char *)"\x80\x01\x00\x02\x00\x00\x00\x04\x69\x73\x4f\x6b\x00\x00\x00\x01\x00";
    p_st_packet->ui_payload_len = 17;

    i_ret = thrift_tbinary_detect(p_st_conn, p_st_packet);

    CU_ASSERT_EQUAL(i_ret, APM_L7_IS_YES);

    frfree(p_st_conn);
    frfree(p_st_packet);

    return;

ERROR:
    if(p_st_conn)
        frfree(p_st_conn);
    if(p_st_packet)
        frfree(p_st_packet);

    return;
}

int add_suite_thrift_tbinary_detect(void)
{
    CU_pSuite pSuite = NULL;

    if ((pSuite = CU_add_suite(MODULE_NAME" thrift_tbinary_detect()", __suite_init, __suite_cleanup)) == NULL)
        return FR_FAIL;

    if (CU_add_test(pSuite, "case1", case1) == NULL ||
        CU_add_test(pSuite, "case2", case2) == NULL ||
        CU_add_test(pSuite, "case3", case3) == NULL)
        return FR_FAIL;

    return FR_SUCC;
}
